﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace AutoChange
{
    public partial class FormAutoChange : Form
    {
        MouseHook hook = new MouseHook();
        int xian = 1;
        int buttonX = 978;
        int buttonY = 527;
        public FormAutoChange()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
        }

        /// <summary>
        /// 指定窗口截图
        /// </summary>
        /// <param name="handle">窗口句柄. (在windows应用程序中, 从Handle属性获得)</param>
        /// <returns></returns>
        public Image CaptureWindow(IntPtr handle)
        {
            IntPtr hdcSrc = Win32API.GetDC(handle);
            Win32API.Rect windowRect;
            Win32API.GetWindowRect(handle, out windowRect);
            int width = windowRect.Right - windowRect.Left;
            int height = windowRect.Bottom - windowRect.Top;
            IntPtr hdcDest = Win32API.CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = Win32API.CreateCompatibleBitmap(hdcSrc, width, height);
            IntPtr hOld = Win32API.SelectObject(hdcDest, hBitmap);
            Win32API.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Win32API.SRCCOPY);
            Win32API.SelectObject(hdcDest, hOld);
            Win32API.DeleteDC(hdcDest);
            Win32API.ReleaseDC(handle, hdcSrc);
            Image img = Image.FromHbitmap(hBitmap);
            Win32API.DeleteObject(hBitmap);
            return img;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            hook = new MouseHook();
            hook.OnMouseActivity += Hook_OnMouseActivity;
            hook.Start();
        }

        private void Hook_OnMouseActivity(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                hook.Stop();
                Thread.Sleep(500);
                IntPtr hwnd = Win32API.GetForegroundWindow();
                textBox1.Text += "," + hwnd.ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("未设置窗口");
                return;
            }
            xian = Convert.ToInt32(comboBox1.Text);

            string[] list = textBox1.Text.Split(',');
            for (int i = 0; i < list.Length; i++)
            {
                if (list[i] != "")
                {
                    IntPtr hwnd = new IntPtr(Convert.ToInt32(list[i]));
                    Thread thread = new Thread(() =>
                    {
                        while (!HuanXian(hwnd))
                        {
                            ClickKey(hwnd, 13);//27为Esc

                        }

                    });
                    thread.Start();

                    ////pictureBox1.Image = CaptureWindow(new IntPtr(Convert.ToInt32(list[i])));
                }
            }
        }

        private string GetWinName(IntPtr hwnd)
        {
            int length = Win32API.GetWindowTextLength(hwnd);
            StringBuilder windowName = new StringBuilder(length + 1);
            Win32API.GetWindowText(hwnd, windowName, windowName.Capacity);
            return windowName.ToString();
        }

        private void ClickKey(IntPtr intPtr, int keyCode)
        {
            Win32API.SendMessage(intPtr, Win32API.WM_KEYDOWN, keyCode, 0);
            Thread.Sleep(5);
            Win32API.SendMessage(intPtr, Win32API.WM_KEYUP, keyCode, 0);
        }

        private void ClickMouse(IntPtr hwnd, int x, int y)
        {
            Win32API.Rect rect;
            Win32API.GetWindowRect(hwnd, out rect);
            Win32API.SendMessage(hwnd, 0x0201, 1, Convert.ToInt32(x - rect.Left + ((y - rect.Top - 20) << 16)));
            Thread.Sleep(5);
            Win32API.SendMessage(hwnd, 0x0202, 1, Convert.ToInt32(x - rect.Left + ((y - rect.Top - 20) << 16)));
        }

        private bool HuanXian(IntPtr hwnd)
        {
            ClickKey(hwnd, 27);//27为Esc
            Thread.Sleep(5);
            ClickMouse(hwnd, buttonX, buttonY);
            Thread.Sleep(10);
            for (int i = 0; i < xian - 1; i++)
            {
                ClickKey(hwnd, 40);//40为down
                Thread.Sleep(5);
            }
            ClickKey(hwnd, 13);//13为Enter
            Thread.Sleep(1000);

            if (GetWinName(hwnd).Contains(xian.ToString() + "线"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void FormAutoChange_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            hook = new MouseHook();
            hook.OnMouseActivity += Hook_ClcikButtonActivity;
            hook.Start();
        }

        private void Hook_ClcikButtonActivity(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                hook.Stop();
                buttonX = e.X;
                buttonY = e.Y;
                MessageBox.Show($"采集完成，坐标为：X={buttonX},Y={buttonY}");
            }
        }

    }
}
